package com.xsy.glsurfaceview

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.PointF
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.mlkit.vision.face.FaceContour
import com.google.mlkit.vision.face.FaceDetection
import com.google.mlkit.vision.face.FaceDetector
import com.google.mlkit.vision.face.FaceDetectorOptions
import com.google.mlkit.vision.face.FaceLandmark
import com.xsy.glsurfaceview.databinding.ActivityMainBinding
class FaceInfo {
    var mCenterLeftEye = PointF(0.0f, 0.0f)
    var mCenterRightEye = PointF(0.0f, 0.0f)

    var mBeginLeftEye = PointF(0.0f, 0.0f)
    var mEndLeftEye = PointF(0.0f, 0.0f)

    var mBeginRightEye = PointF(0.0f, 0.0f)
    var mEndRightEye = PointF(0.0f, 0.0f)

    var mLeftCheekOne = PointF(0.0f, 0.0f)
    var mLeftCheekTwo = PointF(0.0f, 0.0f)
    var mRightCheekOne = PointF(0.0f, 0.0f)
    var mRightCheekTwo = PointF(0.0f, 0.0f)

    var mNoseOne = PointF(0.0f, 0.0f)
    var mNoseTwo = PointF(0.0f, 0.0f)
}

class MainActivity : AppCompatActivity() {


    private lateinit var viewBinding:ActivityMainBinding
    private lateinit var mFaceDetection: FaceDetector
    private var mFaceInfo: FaceInfo = FaceInfo()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        viewBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        faceDetection()
    }
    private fun loadImage(): Bitmap {
        val options = BitmapFactory.Options()
        options.inScaled = false
        return BitmapFactory.decodeResource(this.resources, R.drawable.bgirl, options)
    }

    private fun faceDetection() {
        val bitmap = loadImage()
        val options = FaceDetectorOptions.Builder()
            .setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_FAST)
            .setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_ALL)
            .setContourMode(FaceDetectorOptions.CONTOUR_MODE_ALL)
            .build()
        mFaceDetection = FaceDetection.getClient(options)
        mFaceDetection.process(bitmap, 0).addOnSuccessListener { faces ->
            for(face in faces){
                val leftEyePos = face.getLandmark(FaceLandmark.LEFT_EYE)?.position
                mFaceInfo.mCenterLeftEye = leftEyePos!!
                val rightEyePos = face.getLandmark(FaceLandmark.RIGHT_EYE)?.position
                mFaceInfo.mCenterRightEye = rightEyePos!!

                val leftEyeContour = face.getContour(FaceContour.LEFT_EYE)?.points
                if(leftEyeContour != null){
                    mFaceInfo.mBeginLeftEye = leftEyeContour[0]
                    mFaceInfo.mEndLeftEye = leftEyeContour[8]
                }
                val rightEyeContour = face.getContour(FaceContour.RIGHT_EYE)?.points
                if(rightEyeContour != null){
                    mFaceInfo.mBeginRightEye = rightEyeContour[0]
                    mFaceInfo.mEndRightEye = rightEyeContour[8]
                }

                val noseContour = face.getContour(FaceContour.NOSE_BRIDGE)?.points
                if(noseContour != null) {
                    mFaceInfo.mNoseOne = noseContour[0]
                    mFaceInfo.mNoseTwo = noseContour[1]
                }

                val faceContour = face.getContour(FaceContour.FACE)?.points
                if(faceContour != null){
                    mFaceInfo.mLeftCheekOne = faceContour[25]
                    mFaceInfo.mLeftCheekTwo = faceContour[23]

                    mFaceInfo.mRightCheekOne = faceContour[11]
                    mFaceInfo.mRightCheekTwo = faceContour[13]
                }
                viewBinding.glSurfaceView.setFaceInfo(mFaceInfo)
            }
        }.addOnFailureListener{e ->
            Log.e("MainActivity", e.toString())
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        viewBinding.glSurfaceView.onDestroy()
    }
}